package org.saxion.concurrency

import scala.actors.Actor
import scala.actors.Actor._
import java.util.ArrayList

class ProjectLeader extends Actor {
  def act() {
    loop {
      react {
        case DeveloperMeeting(developers: ArrayList[Actor]) =>
          developerMeeting(developers)
        case CustomerMeeting(customers: ArrayList[Actor]) =>
          customerMeeting(customers)
      }
    }
  }

  def developerMeeting(developers: ArrayList[Actor]) {
    println("ProjectLeader has received a list of developers wanting to meet")
    Thread.sleep(((scala.math.random) * 1000).toLong);
    var itc = developers.iterator
    while (itc.hasNext()) {
      itc.next ! EndMeeting
    }
    sender ! EndMeeting(false)
  }

  def customerMeeting(customers: ArrayList[Actor]) {
    println("ProjectLeader has received a list of customers wanting to meet")
    var it = customers.iterator
    while (it.hasNext()) {
      it.next ! BeginMeeting
    }
    println("ProjectLeader at meeting");
    Thread.sleep(((scala.math.random) * 1000).toLong);
    it = customers.iterator
    while (it.hasNext()) {
      it.next ! EndMeeting
    }
    sender ! EndMeeting(true)
  }
}